package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/6/13 22:29
 */
public class C60 {

    String ans;
    int rest;
    char[] str;

    public String getPermutation(int n, int k) {
        rest = k;
        str = new char[n];
        for (int i = 0; i < n; i++) {
            str[i] = (char) (i + '1');
        }
        process(0, n);
        return ans;
    }

    private void process(int i, int n) {
        if (rest < 0) return;
        if (i == n) {
            if (--rest == 0) {
                ans = new String(str);
            }
        } else {
            for (int j = i; j < n; j++) {
                swap(str, i, j);
                process(i + 1, n);
                swap(str, i, j);
            }
        }
    }

    private void swap(char[] nums, int i, int j) {
        char tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }


    // for test
    public static void main(String[] args) {
        int n = 3;
        int k = 6;
        System.out.println(new C60().getPermutation(n, k));
    }

}
